package com.masterclient.app.businessmodule.minemodule.index.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.bean.personal.CustomerOrderInfo;
import com.eoner.common.config.Config;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.managerlibrary.sobot.SobotManager;
import com.eoner.managerlibrary.user.UserDataManager;
import com.eoner.middlelib.router.intercept.FCBaseIntercept;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.base.FragmentHostActivity;
import com.masterclient.app.base.FrozenDialog;
import com.masterclient.app.businessmodule.usermodule.login.index.QuickLoginActivity;
import com.masterclient.app.businessmodule.minemodule.index.animation.PersonAnimation;
import com.masterclient.app.modules.coupon.CouponActivity;
import com.masterclient.app.utils.CommonUtil;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.enumtype.SobotChatTitleDisplayMode;
import com.sobot.chat.api.model.Information;

public class PersonFunctionView extends FrameLayout implements View.OnClickListener {

    //优惠券
    private LottieAnimationView lottieTask;
    private View ll_yhq;
    //直播数据
    private LinearLayout ll_live_data;
    //领券中心
    private LinearLayout llGetCoupon;
    private ImageView ivUnread;
    //平台客服
    private LinearLayout llKefu;
    //我的导师
    private LinearLayout ll_ds;
    //常见问题
    private LinearLayout ll_issue;
    //已经中心
    private LinearLayout ll_yj;
    //我的收藏
    private LinearLayout llMyCollection;
    //关注店铺
//    private LinearLayout ll_favorite_store;
    //好评
//    private LinearLayout ll_praise;
    //足迹
//    private LinearLayout llZuji;
    //我也要播
//    private LinearLayout ll_live;
    private TextView tvLive;
    //收货地址
    private LinearLayout llAddress;
    //商家入住
    private LinearLayout ll_shop_in;
    //版本信息
    private LinearLayout llVersion;
    //设置
    private LinearLayout llSetting;
    //第三行bg
    private LinearLayout thirdBg;
    //公众号
    private LinearLayout llGzhBtn;

    private PersonFunctionViewItemClickListener iPersonFunctionViewItemClickListener;

    public void setPersonFunctionViewItemClickListener(PersonFunctionViewItemClickListener iPersonFunctionViewItemClickListener) {
        this.iPersonFunctionViewItemClickListener = iPersonFunctionViewItemClickListener;
    }

    private String signUrl;
    private String liveDataUrl;
    private String wechatUrl = "";

    public void setSignUrl(String signUrl) {
        this.signUrl = signUrl;
    }

    public PersonFunctionView(@NonNull Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.layout_person_func,this);
        initUI();
    }

    public PersonFunctionView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_person_func,this);
        initUI();
    }

    private void initUI() {
        //优惠券
        lottieTask = findViewById(R.id.lottie_task);
        ll_yhq = findViewById(R.id.ll_yhq);
        ll_yhq.setOnClickListener(this);
        //直播数据
//        ll_live_data = findViewById(R.id.ll_live_data);
//        ll_live_data.setOnClickListener(this);
        //领券中心
        llGetCoupon = findViewById(R.id.ll_get_coupon);
        llGetCoupon.setOnClickListener(this);
        ivUnread = findViewById(R.id.iv_unread);
        //客服
        llKefu = findViewById(R.id.ll_kefu);
        llKefu.setOnClickListener(this);
        //导师
        ll_ds = findViewById(R.id.ll_ds);
        ll_ds.setOnClickListener(this);
        //导师
        ll_issue = findViewById(R.id.ll_issue);
        ll_issue.setOnClickListener(this);
        ll_yj = findViewById(R.id.ll_yj);
        ll_yj.setOnClickListener(this);
        //收藏
        llMyCollection = findViewById(R.id.ll_my_collection);
        llMyCollection.setOnClickListener(this);
        //关注店铺
//        ll_favorite_store = findViewById(R.id.ll_favorite_store);
//        ll_favorite_store.setOnClickListener(this);
        //好评
//        ll_praise = findViewById(R.id.ll_praise);
//        ll_praise.setOnClickListener(this);
        //足迹
//        llZuji = findViewById(R.id.ll_zuji);
//        llZuji.setOnClickListener(this);
        //我也要播
//        ll_live = findViewById(R.id.ll_live);
//        ll_live.setOnClickListener(this);
        tvLive = findViewById(R.id.tv_live);
        //地址
        llAddress = findViewById(R.id.ll_address);
        llAddress.setOnClickListener(this);
        //商家入住
//        ll_shop_in = findViewById(R.id.ll_shop_in);
//        ll_shop_in.setOnClickListener(this);
        //版本信息
//        llVersion = findViewById(R.id.ll_version);
//        llVersion.setOnClickListener(this);
        //设置
        llSetting = findViewById(R.id.ll_setting);
        llSetting.setOnClickListener(this);

//        thirdBg = findViewById(R.id.ll_third_bg);
        //公众号
//        llGzhBtn = findViewById(R.id.ll_gzh_data);
//        llGzhBtn.setOnClickListener(this);

    }

    public TextView getTvLive() {
        return tvLive;
    }

    public LottieAnimationView getLottieTask() {
        return lottieTask;
    }

    public ImageView getIvUnread() {
        return ivUnread;
    }

    public void refreshData(CustomerOrderInfo.DataBean dataBean, String signUrl, PersonAnimation mPersonAnimation) {
        refreshMessageUnRead(dataBean.isHas_notice());

//        if (dataBean.isIs_anchor()) {
//            getTvLive().setText("我要开播");
//        } else {
//            getTvLive().setText("我也要播");
//        }
        if (dataBean.isMission_is_receive()) {
            getLottieTask().setVisibility(View.VISIBLE);
            mPersonAnimation.playShopAnimation(getContext(),getLottieTask(), "data_task.json");
        } else {
            getLottieTask().setVisibility(View.GONE);
        }
        setSignUrl(signUrl);
//        if (dataBean.getLive_data() != null && dataBean.getLive_data().isIs_show()) {
//            liveDataUrl = dataBean.getLive_data().getLive_data_url();
//            ll_live_data.setVisibility(VISIBLE);
////            thirdBg.setVisibility(VISIBLE);
//        } else {
//            ll_live_data.setVisibility(GONE);
////            thirdBg.setVisibility(GONE);
//        }
        if (dataBean.getGzg_wechat() != null && dataBean.getGzg_wechat().getTarget_url() != null) {
            wechatUrl = dataBean.getGzg_wechat().getTarget_url();
        } else {
            wechatUrl = "";
        }
    }

    /**
    * 更新我的消息红点
    * */
    private void refreshMessageUnRead(boolean notice) {
        if (notice) {
            ivUnread.setVisibility(View.VISIBLE);
        } else {
            ivUnread.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_yhq) {
            //优惠券
            if ("2".equals(Config.FROZEN)) {
                FrozenDialog.showFrozen(getContext());
                return;
            }
            if (UserDataManager.getInstance().isSystemLogin()) {
                startActivitryWithBundle(CouponActivity.class, null);
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        }
//        else if (v.getId() == R.id.ll_live_data) {
//            if (TextUtils.isEmpty(liveDataUrl)) return;
//            startActivitryWithBundle(BridgeActivity.class, new String[][]{{"url", liveDataUrl}});
//        }
        else if (v.getId() == R.id.ll_get_coupon) {
            //领券中心
            if ("2".equals(Config.FROZEN)) {
                FrozenDialog.showFrozen(getContext());
                return;
            }
            if (UserDataManager.getInstance().isSystemLogin()) {
                Fragment fragment = (Fragment) ARouter.getInstance().build(AroutePath.GET_COUPON_FRAGMENT)
                        .withString("type", "unuse")
                        .navigation();
                FragmentHostActivity.openFragment(getContext(),fragment);
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_kefu) {
            //客服
            if (!UserDataManager.getInstance().isSystemLogin()) {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
                return;
            }
            Information info = SobotManager.getBaseInfo();
            //SobotApi.setChatTitleDisplayMode(mContext, SobotChatTitleDisplayMode.ShowFixedText,"个人中心");
            SobotApi.setChatTitleDisplayMode(getContext(), SobotChatTitleDisplayMode.Default, "");
            SobotApi.setFlowType(getContext(), "0");
            SobotApi.startSobotChat(getContext(), info);
        } else if (v.getId() == R.id.ll_ds) {
            //我的导师
            if ("2".equals(Config.FROZEN)) {
                FrozenDialog.showFrozen(getContext());
                return;
            }
            if (UserDataManager.getInstance().isSystemLogin()) {
                ARouter.getInstance().build(FCRouterPath.TUTOR_PATH).navigation(getContext(),new FCBaseIntercept());
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        }else if (v.getId() == R.id.ll_my_collection) {
            //我的收藏
            if ("2".equals(Config.FROZEN)) {
                FrozenDialog.showFrozen(getContext());
                return;
            }
            if (UserDataManager.getInstance().isSystemLogin()) {
                ARouter.getInstance().build(FCRouterPath.MINE_COLLECTION_PATH).navigation(getContext(),new FCBaseIntercept());
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_address) {
            //地址
            if ("2".equals(Config.FROZEN)) {
                FrozenDialog.showFrozen(getContext());
                return;
            }
            if (UserDataManager.getInstance().isSystemLogin()) {
                ARouter.getInstance().build(FCRouterPath.MINE_ADDRESS_PATH).navigation(getContext(),new FCBaseIntercept());
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_setting) {
            //设置
            if ("2".equals(Config.FROZEN)) {
                FrozenDialog.showFrozen(getContext());
                return;
            }
            ARouter.getInstance().build(FCRouterPath.SETTING_PATH).navigation(getContext(),new FCBaseIntercept());
        }else if (v.getId() == R.id.ll_issue) {
            //常见问题
            CommonUtil.goAnyWhere(getContext(), "https://h5-debug.xrkmall.com/help", "", "", "", "");
        }else if (v.getId() == R.id.ll_yj) {
            //意见反馈
            CommonUtil.goAnyWhere(getContext(), "http://brvmr6012qee97f9.mikecrm.com//nBXLnzK", "", "", "", "");
        }
    }

    /**
    * 是否显示直播数据
    * */
    public void isShowLiveData(boolean isShow) {
//        if (isShow) {
//            thirdBg.setVisibility(VISIBLE);
//        } else {
//            thirdBg.setVisibility(GONE);
//        }
    }

    /**
    * 页面跳转
    * */
    private void startActivitryWithBundle(@NonNull Class clazz, String[][] val) {
        Intent intent = new Intent(getContext(), clazz);
        Bundle bundle = new Bundle();
        if (val != null) {
            for (String[] aVal : val) {
                bundle.putString(aVal[0], aVal[1]);
            }
            intent.putExtras(bundle);
        }
        getContext().startActivity(intent);
    }

    private String getAppInfo() {
        try {
            String pkName = getContext().getPackageName();
            String versionName = getContext().getPackageManager().getPackageInfo(
                    pkName, 0).versionName;
            int versionCode = getContext().getPackageManager()
                    .getPackageInfo(pkName, 0).versionCode;
            return versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public interface PersonFunctionViewItemClickListener {
        void onClickLiveItem();
    }
}
